package name.williamwall.buxoid;
/**
 * This software is made available under the Gnu General Public License v3
 * and has absolutely no waranty. You may change the software to suit your needs.
 * 
 * Author: William Wall, IV
 */
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class EditPassword extends Activity {
	private BuxoidDb mDb;
	private TextView mOldPassLabel;
	private EditText mOldPass, mNewPass1, mNewPass2;
	private CheckBox mRemovePass;
	private boolean mOldPassExists;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mDb = new BuxoidDb(this);
		mDb.open();
		
		setContentView(R.layout.edit_password);
		
		mOldPassExists = !mDb.getSetting(BuxoidDb.SETTING_BUXOID_PASS).equals("");
		
		mOldPassLabel = (TextView) findViewById(R.id.edit_password_old_label);
		mOldPass = (EditText) findViewById(R.id.edit_password_old);
		mNewPass1 = (EditText) findViewById(R.id.edit_password_new1);
		mNewPass2 = (EditText) findViewById(R.id.edit_password_new2);
		mRemovePass = (CheckBox) findViewById(R.id.edit_password_remove);
		Button cancel = (Button) findViewById(R.id.edit_password_cancel);
		Button save = (Button) findViewById(R.id.edit_password_save);
		
		
		if (!mOldPassExists) {
			mOldPassLabel.setVisibility(View.GONE);
			mOldPass.setVisibility(View.GONE);
			mRemovePass.setVisibility(View.GONE);
			setTitle("Create a Password");
		} else {
			setTitle("Change Password");
		}
		
		mRemovePass.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
			public void onCheckedChanged (CompoundButton b, boolean isChecked) {
				if (isChecked) {
					mNewPass1.setText("");
					mNewPass1.setEnabled(false);
					mNewPass2.setText("");
					mNewPass2.setEnabled(false);
				} else {
					mNewPass1.setEnabled(true);
					mNewPass2.setEnabled(true);
				}
			}
		});
		
		cancel.setOnClickListener(new View.OnClickListener() {
			public void onClick(View view) {
				finish();
			}
		});
		
		save.setOnClickListener(new View.OnClickListener() {
			public void onClick(View view) {
				if (mOldPassExists) {
					String old = mOldPass.getText().toString();
					if (Buxoid.getMd5Hash(old).equals(mDb.getSetting(BuxoidDb.SETTING_BUXOID_PASS))) {
						if (changeOrRemovePassword()) {
							finish();
						}
					} else {
						Toast.makeText(EditPassword.this, "Old password incorrect", Toast.LENGTH_SHORT).show();
					}
				} else {
					if (changeOrRemovePassword()) {
						finish();
					}
				}
			}
		});
	}

	private boolean changeOrRemovePassword() {
		String p1 = mNewPass1.getText().toString();
		String p2 = mNewPass2.getText().toString();
		
		if (!mRemovePass.isChecked()) {
			if (p1.equals(p2) ) {
				if (p1.length() >= 6) {
					mDb.putSetting(BuxoidDb.SETTING_BUXOID_PASS, Buxoid.getMd5Hash(p1));
					Toast.makeText(this, "Password saved", Toast.LENGTH_SHORT).show();
					return true;
				} else {
					Toast.makeText(this, "Password must be at least 6 characters.", Toast.LENGTH_SHORT).show();
					return false;
				}
			} else {
				Toast.makeText(this, "Passwords do not match.", Toast.LENGTH_SHORT).show();
				return false;
			}
		} else {
			// remove password
			mDb.putSetting(BuxoidDb.SETTING_BUXOID_PASS, "");
			return true;
		}
	}
}
